package com.iceberg.database.dictionary;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DictionaryDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public DictionaryPO getById(String urid) {
        String sql = String.format("SELECT t.* FROM TSYS_DICTIONARY t WHERE t.URID = '%s'", urid);
        List<DictionaryPO> dictionaryList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DictionaryPO.class));
        return CollectionUtil.isEmpty(dictionaryList) ? null : dictionaryList.get(0);
    }

    public int insert(DictionaryPO dictionaryPO) {
        String sql = "INSERT INTO TSYS_DICTIONARY (URID, CODE, NAME, VALUE, ORDER_NUM, SYS_NAME) VALUES (?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, dictionaryPO.getUrid(), dictionaryPO.getCode(), dictionaryPO.getName(),
                dictionaryPO.getValue(), dictionaryPO.getOrderNum(), dictionaryPO.getSysName());
    }

    public int update(DictionaryPO dictionaryPO) {
        String sql = "UPDATE TSYS_DICTIONARY T SET CODE = ?, NAME = ?, VALUE = ?, ORDER_NUM = ? WHERE URID = ?";
        return jdbcTemplate.update(sql, dictionaryPO.getCode(), dictionaryPO.getName(),
                dictionaryPO.getValue(), dictionaryPO.getOrderNum(), dictionaryPO.getUrid());
    }

    public int delete(String urid) {
        String sql = "DELETE FROM TSYS_DICTIONARY WHERE URID = ?";
        return jdbcTemplate.update(sql, urid);
    }
}
